<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class master extends CI_Controller {

    function __construct(){
    parent::__construct();
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("If-Modified-Since: Mon, 22 Jan 2008 00:00:00 GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Cache-Control: private");
		header("Pragma: no-cache");
		$this->auth	= unserialize(base64_decode($this->session->userdata('sisfodealer')));
		$this->load->model(array('mmaster','mdealer'));
	}

	public function index(){
		if($this->auth){
			$data['module'] = "master";
			$this->load->view('home', $data);
		}else{
			$data['pesan'] = "<font color='red'>anda harus login!</font>";
			$this->load->view('login',$data);
			return;
		}
	}
	
	function getdisplay($param=""){
		$data['type'] = $param;
		$this->load->view('master/master_mng',$data);
	}
	
	function gettab(){
		$type = $this->input->post('type');
		switch($type){
			case 'wilayah':
				$data['optprovinsi']  = $this->combobox('idx_wilayah_provinsi','', 'return');
				$data['optkabupaten'] = $this->combobox('idx_wilayah_kabupaten','', 'return');
				$data['optkecamatan'] = $this->combobox('idx_wilayah_kecamatan','', 'return');
				$data['optkelurahan'] = $this->combobox('idx_wilayah_kelurahan','', 'return');
			break;
			case 'dealer':
				$data['optgroupdealer'] = $this->combobox('idx_group_dealer','', 'return');
			break;
			case 'supplier':
				$data['optgroupsupplier'] = $this->combobox('idx_group_supplier','', 'return');
			break;
			case 'customer':
				$data['optgroupcustomer'] = $this->combobox('idx_group_customer','', 'return');
			break;
			case 'gudang':
				$data['optdealer'] = $this->combobox('tbl_master_dealer','', 'return');
			break;
			case 'karyawan':
				$data['optdealer'] = $this->combobox('tbl_master_dealer','', 'return');
				$data['optdivisi'] = $this->combobox('idx_divisi',"", 'return');
				$data['optagama']  = $this->combobox('idx_agama',"", 'return');
			break;
			case 'pihak':
				$data['optagama']  = $this->combobox('idx_agama',"", 'return');
			break;

			// master proses
			case "proses_promosi_unit":
				
			break;

			case "proses_promosi_service":
			break;

			case "proses_promosi_sparepart":
			break;

			case "proses_bbn_unit":
			break;

			case "proses_pembiayaan_unit":
			break;

			case "proses_service_ksg":
			break;

			// master akutansi
			case "kode_akun":
			break;

			case "akun_kas":
			break;

			case "akun_barang_jasa":
			break;

			case "akun_aktifa_tetap":
			break;

			case "akun_saldo":
			break;			
		}
		$data['type'] = $type;
		$this->load->view('master/master_tab',$data);
	}
	
	function getdata($param=""){
		echo $this->mmaster->getdata($param);
	}
	
	function getdatatambahan($type){
		$param1 = $this->input->post('param1');
		$param2 = $this->input->post('param2');
		$param3 = $this->input->post('param3');
		echo $this->mmaster->getdatatambahan($type, $param1, $param2, $param3);
	}
	
	function savedata($type){
		$post = array();
        foreach($_POST as $k=>$v) $post[$k] = $this->input->post($k);
		
		/*print_r($post);
		exit;*/

		$editstatus = $post['editstatus'];
		unset($post['editstatus']);
		if($editstatus == 'add'){
			unset($post['id']);
		}		
		echo $this->mmaster->savedatamaster($post,$type,$editstatus);
	}
	
	function getform($editstatus="", $type=""){		
		if($editstatus == 'edit'){
			$id = $this->input->post('id');
			$datarecord = $this->mmaster->getrecord($type, $id);
			$data['datarecord'] = $datarecord;
		}
		switch($type){
			case 'wilayah':
				$data['optprovinsi']  = $this->combobox('idx_wilayah_provinsi',($editstatus == 'edit' ? $datarecord->kode_provinsi : ''), 'return');
				$data['optkabupaten'] = $this->combobox('idx_wilayah_kabupaten',($editstatus == 'edit' ? $datarecord->kode_kabupaten : ''), 'return');
				$data['optkecamatan'] = $this->combobox('idx_wilayah_kecamatan',($editstatus == 'edit' ? $datarecord->kode_kecamatan : ''), 'return');
				$data['optkelurahan'] = $this->combobox('idx_wilayah_kelurahan',($editstatus == 'edit' ? $datarecord->kode_kelurahan : ''), 'return');
			break;
			case 'dealer':
				$data['optgroupdealer']  		= $this->combobox('idx_group_dealer',($editstatus == 'edit' ? $datarecord->id_group_dealer : ''), 'return');
				$data['optwilayah'] 	 		= $this->combobox('tbl_master_wilayah',($editstatus == 'edit' ? $datarecord->id_wilayah : ''), 'return');
				$data['optstatus'] 		 		= $this->combobox('status_dealer',($editstatus == 'edit' ? $datarecord->status_dealer : ''), 'return');
				$data['optsystemstatus']		= $this->combobox('status',($editstatus == 'edit' ? $datarecord->status_system : ''), 'return');
				$data['optstatusperusahaan']  	= $this->combobox('status_usaha',($editstatus == 'edit' ? $datarecord->status_perusahaan : ''), 'return');
			break;
			case 'supplier':
				$data['optgroupsupplier'] = $this->combobox('idx_group_supplier',($editstatus == 'edit' ? $datarecord->id_group_supplier : ''), 'return');
				$data['optwilayah'] 	= $this->combobox('tbl_master_wilayah',($editstatus == 'edit' ? $datarecord->id_wilayah : ''), 'return');
				$data['optstatus'] 		= $this->combobox('status',($editstatus == 'edit' ? $datarecord->status_supplier : ''), 'return');
			break;
			case 'customer':
				$data['optgroupcustomer'] = $this->combobox('idx_group_customer',($editstatus == 'edit' ? $datarecord->id_group_customer : ''), 'return');
				$data['optagama'] = $this->combobox('idx_agama',($editstatus == 'edit' ? $datarecord->id_agama : ''), 'return');
				$data['optststmptinggal'] = $this->combobox('idx_status_tempat_tinggal',($editstatus == 'edit' ? $datarecord->id_status_tmp_tinggal_customer : ''), 'return');
				$data['optpekerjaan'] = $this->combobox('idx_jenis_pekerjaan',($editstatus == 'edit' ? $datarecord->id_pekerjaan : ''), 'return');
				
				if($editstatus == 'edit'){
					$exptgl = explode('-', $datarecord->tanggal_lahir);
					$tahun  = $exptgl[0];
					$bulan  = $exptgl[1];
					$tanggal = $exptgl[2];
				}
				
				$data['opttanggal'] 	= $this->combobox('tanggal',($editstatus == 'edit' ? $tanggal : ''), 'return');
				$data['optbulan']		= $this->combobox('bulan',($editstatus == 'edit' ? $bulan : ''), 'return');
				$data['opttahun']		= $this->combobox('tahun',($editstatus == 'edit' ? $tahun : ''), 'return');
			break;
			case 'gudang':
				$data['optgroupdealer'] = $this->combobox('idx_group_dealer',($editstatus == 'edit' ? $datarecord->id_group_dealer : ''), 'return');
				$data['optdealer'] 		= $this->combobox('tbl_master_dealer',($editstatus == 'edit' ? $datarecord->id_dealer : ''), 'return');
				$data['optdivisi'] 		= $this->combobox('idx_divisi',($editstatus == 'edit' ? $datarecord->id_divisi : ''), 'return');
				$data['optstatus'] 		= $this->combobox('status',($editstatus == 'edit' ? $datarecord->status_gudang : ''), 'return');
			break;
			case 'karyawan':
				$foto = '<img src="'.base_url().'assets/images/nofoto.png">';
				$laki_laki = "";
				$perempuan = "";
				$wni	= "";
				$wna	= "";
				$M		= "";
				$BM		= "";
				$JD		= "";
				if($editstatus == 'edit'){
					if($datarecord->jenis_kelamin == "L") $laki_laki = "checked='true'";
					if($datarecord->jenis_kelamin == "P") $perempuan = "checked='true'";
					if($datarecord->kewarganegaraan == "WNI") $wni = "checked='true'";
					if($datarecord->kewarganegaraan == "WNA") $wna = "checked='true'";
					if($datarecord->status_kawin == "M") $M = "checked='true'";
					if($datarecord->status_kawin == "BM") $BM = "checked='true'";
					if($datarecord->status_kawin == "JD") $JD = "checked='true'";
					
					$exptgl = explode('-', $datarecord->tanggal_lahir);
					$tahun  = $exptgl[0];
					$bulan  = $exptgl[1];
					$tanggal = $exptgl[2];
					
					if($datarecord->foto_karyawan){ $foto = '<img src="'.base_url().'repository/master/foto_karyawan/'.$datarecord->foto_karyawan.'" height="270" width"260">'; }
				}
				
				$data['lelaki'] = $laki_laki;
				$data['perempuan'] = $perempuan;
				$data['wni'] = $wni;
				$data['wna'] = $wna;
				$data['M'] = $M;
				$data['BM'] = $BM;
				$data['JD'] = $JD;
				
				$data['foto'] = $foto;
				
				$data['opttanggal'] 	= $this->combobox('tanggal',($editstatus == 'edit' ? $tanggal : ''), 'return');
				$data['optbulan']		= $this->combobox('bulan',($editstatus == 'edit' ? $bulan : ''), 'return');
				$data['opttahun']		= $this->combobox('tahun',($editstatus == 'edit' ? $tahun : ''), 'return');
				
				$data['optagama'] 		= $this->combobox('idx_agama',($editstatus == 'edit' ? $datarecord->id_agama : ''), 'return');
				$data['optstskaryawan'] = $this->combobox('idx_status_karyawan',($editstatus == 'edit' ? $datarecord->id_status_karyawan : ''), 'return');
				$data['optgroupdealer'] = $this->combobox('idx_group_dealer',($editstatus == 'edit' ? $datarecord->id_group_dealer : ''), 'return');
				$data['optdealer'] 		= $this->combobox('tbl_master_dealer',($editstatus == 'edit' ? $datarecord->id_dealer : ''), 'return');
				$data['optdivisi'] 		= $this->combobox('idx_divisi',($editstatus == 'edit' ? $datarecord->id_divisi : ''), 'return');
				$data['optststmptinggal'] = $this->combobox('idx_status_tempat_tinggal',($editstatus == 'edit' ? $datarecord->id_status_tmp_tinggal_karyawan : ''), 'return');
			break;
			case 'pihak':
				$laki_laki = "";
				$perempuan = "";
				$wni	= "";
				$wna	= "";
				$M		= "";
				$BM		= "";
				$JD		= "";
				if($editstatus == 'edit'){
					if($datarecord->jenis_kelamin == "L") $laki_laki = "checked='true'";
					if($datarecord->jenis_kelamin == "P") $perempuan = "checked='true'";
					if($datarecord->kewarganegaraan == "WNI") $wni = "checked='true'";
					if($datarecord->kewarganegaraan == "WNA") $wna = "checked='true'";
					if($datarecord->status_kawin == "M") $M = "checked='true'";
					if($datarecord->status_kawin == "BM") $BM = "checked='true'";
					if($datarecord->status_kawin == "JD") $JD = "checked='true'";
					
					$exptgl = explode('-', $datarecord->tanggal_lahir);
					$tahun  = $exptgl[0];
					$bulan  = $exptgl[1];
					$tanggal = $exptgl[2];					
				}
				
				$data['lelaki'] = $laki_laki;
				$data['perempuan'] = $perempuan;
				$data['wni'] = $wni;
				$data['wna'] = $wna;
				$data['M'] = $M;
				$data['BM'] = $BM;
				$data['JD'] = $JD;
				
				$data['opttanggal'] 	= $this->combobox('tanggal',($editstatus == 'edit' ? $tanggal : ''), 'return');
				$data['optbulan']		= $this->combobox('bulan',($editstatus == 'edit' ? $bulan : ''), 'return');
				$data['opttahun']		= $this->combobox('tahun',($editstatus == 'edit' ? $tahun : ''), 'return');
				
				$data['optagama'] 		= $this->combobox('idx_agama',($editstatus == 'edit' ? $datarecord->id_agama : ''), 'return');
				$data['optststmptinggal'] = $this->combobox('idx_status_tempat_tinggal',($editstatus == 'edit' ? $datarecord->id_status_tempat_tinggal : ''), 'return');
			break;
			case 'kas':
			case 'aktiva':
				$data['optgroupdealer'] = $this->combobox('idx_group_dealer',($editstatus == 'edit' ? $datarecord->id_group_dealer : ''), 'return');
				$data['optdealer'] 		= $this->combobox('tbl_master_dealer',($editstatus == 'edit' ? $datarecord->id_dealer : ''), 'return');
			break;
			case 'warna':
				$data['optstatuswarna'] = $this->combobox('status',($editstatus == 'edit' ? $datarecord->status : ''), 'return');;
			break;
			case 'service':
				$data['optgroupservice'] 	= $this->combobox('idx_group_service',($editstatus == 'edit' ? $datarecord->id_group_service : ''), 'return');
				$data['optkategoriservice'] = $this->combobox('idx_kategori_service',($editstatus == 'edit' ? $datarecord->id_kategori_service : ''), 'return');
				$date['opttypeservice']		= $this->combobox('idx_kategori_service',($editstatus == 'edit' ? $datarecord->id_kategori_service : ''), 'return');
			break;
			case 'motor':
			case 'sparepart':
			case 'generator':
			case 'peralatan_service':
			case 'barang_promosi':
				for ($j = 0; $j <= 1; $j++) {
					$data['foto'][$j] 	 			= '<img src="'.base_url().'assets/images/no_image.jpg" height="270" width"260">';
					$data['id_foto'][$j]  			= "";
					$data['keterangan_foto'][$j] 	= "";
				}
				
				if($editstatus == 'edit') {
					$datarecord2 = $this->mmaster->getrecord('harga_jual', $datarecord->id);
					
					$rest = array();
					$ct = 0;
					foreach ($datarecord2 as $dtx) {
						$dtx['optkondisippn'] = $this->combobox('kondisi_ppn',($editstatus == 'edit' ? $dtx['kondisi_ppn_hrg_jual'] : ''), 'return');
						$dtx['opttingkathrgjual'] = $this->combobox('tingkat_hrg_jual',($editstatus == 'edit' ? $dtx['tingkat_hrg_jual'] : ''), 'return');
						$rest[$ct] = $dtx;
						$ct++;
					}
					$data['hrg_jual'] = $rest;
					
					/*echo "<pre>";
					print_r($data['hrg_jual']);
					echo "</pre>";
					die();*/

					if ($type == "motor") {
						//$data['hrgjual'] = $this->mmaster->getrecord("harga_jual", $datarecord2);
					} else {
						$rs = $this->mmaster->getrecord("gambar_barang", $datarecord->kode_gambar);
						$ct = 0;
						foreach($rs as $r=>$v) {
							$data['foto'][$ct] 				= '<img src="'.base_url().'repository/master/foto_barang/'.$v['file_name'].'" height="270" width"260">';
							$data['id_foto'][$ct] 			= $v['id'];
							$data['keterangan_foto'][$ct] 	= $v['keterangan_gambar'];
							$ct++;
						}
					}
				}
				
				$data['opttingkathrgjual'] = $this->combobox('tingkat_hrg_jual','', 'return');
				$data['optkondisippnjual'] = $this->combobox('kondisi_ppn','', 'return');
				$data['optgroupbarang']    = $this->combobox('idx_group_barang',($editstatus == 'edit' ? $datarecord->id_group_barang : ''), 'return');
				$data['optkondisippnbeli'] = $this->combobox('kondisi_ppn',($editstatus == 'edit' ? $datarecord->kondisi_ppn_harga_beli : ''), 'return');
				$data['optkategoribarang'] = $this->combobox('idx_kategori_barang',($editstatus == 'edit' ? $datarecord->id_kategori_barang : ''), 'return');
			break;

			// master proses
			case "proses_promosi_unit":
				if($editstatus == 'edit') {
					//====================get dealer=====================
					$rs_dealer = $this->mmaster->getrecord('promosi_dealer', $datarecord->id);
					$rest = array();
					$ct = 0;
					foreach($rs_dealer as $rd) {
						$rd['optgroupdealer'] = $this->combobox('tbl_master_dealer',($editstatus == 'edit' ? $rd['group_promosi_dealer'] : ''), 'return');
						$rest[$ct] = $rd;
						$ct++;
					}
					$data['prodealer'] = $rest;

					//=========================get gift data=========================
					$rs_hadiah = $this->mmaster->getrecord('promosi_hadiah', $datarecord->id);
					$rhadiah = array();
					$cth = 0;
					foreach($rs_hadiah as $hdh) {
						$rhadiah[$cth] = $hdh;
						$cth++;
					}
					$data['hadiah'] = $rhadiah;

					//====================get dealer credit data======================
					$rs_kdealer = $this->mmaster->getrecord('kredit_dealer', $datarecord->id);
					$rkdealer = array();
					$cth = 0;

					foreach($rs_kdealer as $kd) {
						$rkdealer[$cth] = $kd;
						$cth++;
					}
					$data['kdealer'] = $rkdealer;

					//===============get dealer credit leasing==============
					$rs_kleasing = $this->mmaster->getrecord('kredit_leasing', $datarecord->id);
					$rkleasing = array();
					$cth = 0;

					foreach($rs_kleasing as $kd) {
						$rkleasing[$cth] = $kd;
						$cth++;
					}
					$data['kleasing'] = $rkleasing;
				}

				$data['optalldealer'] 	= $this->combobox('all_dealer',($editstatus == 'edit' ? $datarecord->promosi_all_dealer : ''), 'return');
				$data['optjenispenjualan'] 	= $this->combobox('jenis_penjualan',($editstatus == 'edit' ? $datarecord->jenis_penjualan : ''), 'return');
				$data['optdealerpromo'] = $this->combobox('tbl_master_dealer','', 'return');
			break;

			case "proses_promosi_service":
				$data['optgroupdealer']  	= $this->combobox('idx_group_dealer',($editstatus == 'edit' ? $datarecord->id_group_dealer : ''), 'return');
				$data['optgroupbarang']  	= $this->combobox('idx_group_barang',($editstatus == 'edit' ? $datarecord->id_group_barang : ''), 'return');
				$data['optkategoribarang']  = $this->combobox('idx_kategori_barang',($editstatus == 'edit' ? $datarecord->id_kategori_barang : ''), 'return');
				$data['optgroupservice'] 	= $this->combobox('idx_group_service',($editstatus == 'edit' ? $datarecord->id_group_service : ''), 'return');
				$data['optsatuan'] 		 	= $this->combobox('satuan',($editstatus == 'edit' ? '' : ''), 'return');
			
				if($editstatus == 'edit') {
					//=========================get data hadiah langsung=========================
					$rs_hadiah = $this->mmaster->getrecord('hadiah_lsg', $datarecord->id);
					$rhadiah = array();
					$cth = 0;
					foreach($rs_hadiah as $hdh) {
						$hdh['optsatuan'] = $this->combobox('satuan',($editstatus == 'edit' ? $hdh['satuan'] : ''), 'return');
						$hdh['optkategoribarang'] = $this->combobox('idx_kategori_barang',($editstatus == 'edit' ? $hdh['kategori_barang_hadiah'] : ''), 'return');
						$rhadiah[$cth] = $hdh;
						$cth++;
					}
					$data['hadiah_lsg'] = $rhadiah;
				}
				
			break;

			case "proses_promosi_sparepart":
				if($editstatus == 'edit') {
					//=========================get gift data=========================
					$rs_hadiah = $this->mmaster->getrecord('paket', $datarecord->id);
					$rhadiah = array();
					$cth = 0;
					foreach($rs_hadiah as $hdh) {
						$hdh['optsatuan'] = $this->combobox('satuan',($editstatus == 'edit' ? $hdh['satuan_barang'] : ''), 'return');
						$rhadiah[$cth] = $hdh;
						$cth++;
					}
					$data['paket'] = $rhadiah;
				}

				$data['optsatuan'] 	= $this->combobox('satuan',($editstatus == 'edit' ? '' : ''), 'return');
			break;

			case "proses_bbn_unit":
			break;

			case "proses_pembiayaan_unit":
				if($editstatus == 'edit') {
					//====================get subsidi======================
					$rs_sub  = $this->mmaster->getrecord('support_subsidi', $datarecord->id);
					$arr_sub = array();
					$cth = 0;

					foreach($rs_sub as $rs) {
						$arr_sub[$cth] = $rs;
						$cth++;
					}
					$data['subsidi'] = $arr_sub;

					//====================get additional subsidi======================
					$rs_sa = $this->mmaster->getrecord('support_subsidi_add', $datarecord->id);
					$arr_sa = array();
					$cth = 0;

					foreach($rs_sa as $sa) {
						$arr_sa[$cth] = $sa;
						$cth++;
					}
					$data['subsidi_add'] = $arr_sa;

					//====================get incentive support======================
					$rs_incentive = $this->mmaster->getrecord('support_incentive', $datarecord->id);
					$arr_incentive = array();
					$cth = 0;

					foreach($rs_incentive as $kd) {
						$arr_incentive[$cth] = $kd;
						$cth++;
					}
					$data['incentive'] = $arr_incentive;

					//====================get promo support======================
					$rs_promo = $this->mmaster->getrecord('support_promo', $datarecord->id);
					$arr_promo = array();
					$cth = 0;

					foreach($rs_promo as $pr) {
						$arr_promo[$cth] = $pr;
						$cth++;
					}
					$data['promo'] = $arr_promo;
				}


				$data['optgroupsupplier'] =  $this->combobox('idx_group_supplier',($editstatus == 'edit' ? $datarecord->id_group_supplier : ''), 'return');
				$data['optjenissupport'] =  $this->combobox('jenis_support',($editstatus == 'edit' ? $datarecord->id_jenis_support : ''), 'return');
			break;

			case "proses_service_ksg":
			break;

			// master akutansi
			case "kode_akun":
				$data['optstatus'] 	= $this->combobox('status',($editstatus == 'edit' ? $datarecord->status : ''), 'return');
				$data['optakuntype'] = $this->combobox('idx_akun_type',($editstatus == 'edit' ? $datarecord->type_akun : ''), 'return');
			break;

			case "akun_kas":
				$data['optgroupdealer'] = $this->combobox('idx_group_dealer',($editstatus == 'edit' ? $datarecord->id_group_dealer : ''), 'return');
				$data['optnamadealer'] = $this->combobox('tbl_master_dealer',($editstatus == 'edit' ? $datarecord->id_dealer : ''), 'return');
				$data['optnamakas'] = $this->combobox('tbl_master_dealer',($editstatus == 'edit' ? $datarecord->id_kas : ''), 'return');
				$data['optakuntype'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->type_akun : ''), 'return');
				$data['optbagianakun'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->bagian_dari_akun : ''), 'return');
				$data['optsubakun'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->bagian_dari_sub_akun : ''), 'return');
			break;

			case "akun_barang_jasa":
				$data['persediaan'] = "";
				$data['nonpersediaan'] = "";
				$data['jasa'] = "";

				if($editstatus == 'edit') {
					if($datarecord->id_tipe_barang == "1") $data['persediaan'] = "checked='true'";
					if($datarecord->id_tipe_barang == "2") $data['nonpersediaan'] = "checked='true'";
					if($datarecord->id_tipe_barang == "3") $data['jasa'] = "checked='true'";
				}

				$data['optgroupbarang'] = $this->combobox('idx_group_barang',($editstatus == 'edit' ? $datarecord->id_group_barang : ''), 'return');
				$data['optkategoribarang'] = $this->combobox('idx_kategori_barang',($editstatus == 'edit' ? $datarecord->id_kategori_barang : ''), 'return');

				$data['optakunpersediaan'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_persediaan : ''), 'return');
				$data['optakunjual'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_jual : ''), 'return');
				$data['optakunreturjual'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_retur_jual : ''), 'return');
				$data['optakundiskonjual'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_diskon_jual : ''), 'return');
				$data['optakunbarangkirim'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_brg_terkirim : ''), 'return');
				$data['optakunhpp'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_hpp : ''), 'return');
				$data['optakunreturbeli'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_retur_beli : ''), 'return');
				$data['optakunbelumtertagih'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_belum_tertagih : ''), 'return');
			break;

			case "akun_aktifa_tetap":
				$data['optgroupdealer'] = $this->combobox('idx_group_dealer',($editstatus == 'edit' ? $datarecord->id_group_dealer : ''), 'return');
				$data['optnamadealer'] = $this->combobox('tbl_master_dealer',($editstatus == 'edit' ? $datarecord->id_dealer : ''), 'return');
				
				$data['optakunaktiva'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_aktiva : ''), 'return');
				$data['optakunpenyusutan'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_akum_susut : ''), 'return');
				$data['optbebatpenyusutan'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->kd_akun_bebat_susut : ''), 'return');
				$data['optopenbalanceqty'] = $this->combobox('tbl_master_kode_akun',($editstatus == 'edit' ? $datarecord->open_balance_qty : ''), 'return');

				$data['opttypeaktiva'] = $this->combobox('type_aktiva',($editstatus == 'edit' ? $datarecord->type_aktiva : ''), 'return');
			break;

			case "akun_saldo":
			break;
		}
		
		$data['tinggiregion'] 	= $this->input->post('tinggiregion');
		$data['editstatus'] 	= $editstatus;
		$data['type']			= $type;
		$this->load->view('master/master_form', $data);
	}
	
	function combobox($type, $param="", $balikan=''){
		$this->load->helper('main_helper');
		$optTemp = "";
		if($param != ""){
			$p2 = $param;
		}else{
			$p2 = "";
		}
		
		if($balikan == ""){
			$balikan = $this->input->post('balikan');
		}else{
			$balikan = $balikan;
		}
		
		$param2 = "";
		if($type == 'idx_kategori_barang' || $type == 'idx_kategori_service' || $type == 'tbl_master_dealer' || $type == 'tbl_master_kas'){
			$param2 = $this->input->post('v');
		}

		if($type == 'status'){
			$data = array(
				'0' => array(
					'code' => 'Y',
					'value' => 'Aktif'
				),
				'1' => array(
					'code' => 'N',
					'value' => 'Tidak Aktif'
				),
			);
		}elseif($type == 'kondisi_ppn'){
			$data = array(
				'0' => array(
					'code' => 'I',
					'value' => 'Include'
				),
				'1' => array(
					'code' => 'E',
					'value' => 'Exclude'
				),
			);
		}elseif($type == 'satuan'){
			$data = array(
				'0' => array(
					'code' => 'pcs',
					'value' => 'Pcs'
				),
				'1' => array(
					'code' => 'box',
					'value' => 'Box'
				),
				'2' => array(
					'code' => 'unit',
					'value' => 'Unit'
				),
			);
		}elseif($type == 'status_dealer'){
			$data = array(
				'0' => array(
					'code' => '1S',
					'value' => 'Sales'
				),
				'1' => array(
					'code' => '2S',
					'value' => 'Service-Spare Part'
				),
				'2' => array(
					'code' => '3S',
					'value' => 'Sales/Service-Spare Part'
				),
			);
		}elseif($type == 'status_usaha'){
			$data = array(
				'0' => array(
					'code' => 'pt',
					'value' => 'P.T.'
				),
				'1' => array(
					'code' => 'cv',
					'value' => 'C.V.'
				),
				'2' => array(
					'code' => 'ud',
					'value' => 'U.D.'
				),
			);
		}elseif($type == 'tanggal' || $type == 'bulan' || $type == 'tahun'){
			$data = arraydate($type);
		/*} elseif($type == 'idx_kategori_barang' && $param == '') {
			$balikan = 'echo';
			$kode_group_barang = $this->input->post('kode_group_barang');
			$data = $this->mmaster->getcombo($type, $param, $kode_group_barang);*/
		}elseif($type=='type_aktiva'){
			$data = array(
				'0'=>array(
					'code'=>'1',
					'value'=>'Tanah'
				),
				'1'=>array(
					'code'=>'2',
					'value'=>'Bangunan'
				),
				'2'=>array(
					'code'=>'3',
					'value'=>'Kendaraan'
				),	
				'3'=>array(
					'code'=>'4',
					'value'=>'Peralatan Bengkel'
				),	
				'4'=>array(
					'code'=>'5',
					'value'=>'Perabotan Kantor'
				),		
			);
		}elseif($type=='all_dealer'){
			$data = array(
				'0'=>array(
					'code'=>'Y',
					'value'=>'Ya'
				),
				'1'=>array(
					'code'=>'N',
					'value'=>'Tidak'
				),
			);
		}elseif($type=='jenis_penjualan'){
			$data = array(
				'0'=>array(
					'code'=>'T',
					'value'=>'Tunai'
				),
				'1'=>array(
					'code'=>'KD',
					'value'=>'Kredit Delaer'
				),
				'2'=>array(
					'code'=>'KL',
					'value'=>'Kredit Leasing'
				),
			);
		}elseif($type=='tingkat_hrg_jual'){
			$data = array(
				'0'=>array(
					'code'=>'1',
					'value'=>'Tingkat 1'
				),
				'1'=>array(
					'code'=>'2',
					'value'=>'Tingkat 2'
				),
				'2'=>array(
					'code'=>'3',
					'value'=>'Tingkat 3'
				),
				'3'=>array(
					'code'=>'4',
					'value'=>'Tingkat 4'
				),
				'4'=>array(
					'code'=>'5',
					'value'=>'Tingkat 5'
				),
				'5'=>array(
					'code'=>'6',
					'value'=>'Tingkat 6'
				),
				'6'=>array(
					'code'=>'7',
					'value'=>'Tingkat 7'
				),
				'7'=>array(
					'code'=>'8',
					'value'=>'Tingkat 8'
				),
				'8'=>array(
					'code'=>'9',
					'value'=>'Tingkat 9'
				),
			);
		}elseif($type=='jenis_support'){
			$data = array(
				'0'=>array(
					'code'=>'S',
					'value'=>'Subsidi'
				),
				'1'=>array(
					'code'=>'I',
					'value'=>'Incentive'
				),
				'2'=>array(
					'code'=>'P',
					'value'=>'Program Promo'
				)
			);
		}else{
			$data = $this->mmaster->getcombo($type, $param, $param2);
		}
		
		$optTemp .= "<option value=''>-- Pilih --</option>";			
		foreach($data as $v=>$k){
			if($p2 == $k['code']){
				$optTemp .= "<option selected value='".$k['code']."'>".$k['value']."</option>";
			}else{
				$optTemp .= "<option value='".$k['code']."'>".$k['value']."</option>";
			}
		}
		
		if($balikan == 'echo'){
			echo $optTemp;
		}elseif($balikan == 'return'){
			return $optTemp;
		}
		
	}
	
	function membantu(){
		$this->load->helper('main_helper');
		echo "<pre>";
		print_r(arraydate('tanggal'));
		echo "</pre>";
	}

	function removedata($type="") {
		$id = $this->input->post('id');
		if ($type == 'sparepart' || $type == 'generator' || 
			$type == 'peralatan_service' || $type == 'barang_promosi') {
				$kd_gambar = $this->input->post('kdgambar');
				$result = $this->mmaster->removedata('gambar_barang', $kd_gambar);
			}
			
		$result = $this->mmaster->removedata($type, $id);
		echo $result ? '1' : '0';
	}
}